//
//  MainWindowRootManager.m
//  MXApp
//
//  Created by CC on 2019/6/1.
//  Copyright © 2019 Shanghai YingXiang Network Technology Co., Ltd. All rights reserved.
//

#import "MainWindowRootManager.h"
//#import "RegisterLoginLogicController.h"
#import "CCNavigationController.h"
#import "HomeViewController.h"
#import "NowViewController.h"
#import "ProfileViewController.h"
#import "ApiRequestUtil.h"

@interface MainWindowRootManager () <LcNotificationDelegate>

@end

@implementation MainWindowRootManager

- (void)internalInit {
  [[LcNotificationCenter sharedInstance] registerDelegate:self];
}

- (void)startAppMainWindow {
  self.mainWindow.rootViewController = [self generateMainViewController];
}

- (UIViewController*)generateMainViewController {
  CCNavigationController* homeNavigaionController;
  CCNavigationController* nowNavigaionController;
  CCNavigationController* profileNavigaionController;

  NSMutableArray* viewControllers = [NSMutableArray arrayWithCapacity:3];

  {
    // home
    HomeViewController* viewController = [[HomeViewController alloc] init];
    viewController.isTabBarChildController = YES;
    viewController.edgesForExtendedLayout = UIRectEdgeTop | UIRectEdgeLeft | UIRectEdgeRight;
    homeNavigaionController = [CCNavigationController
        transparentNavigationControllerWithRootViewController:viewController];
    UITabBarItem* tabBarItem = [self tabBarItem:@"首页"
                                      imageName:@"tab1_"
                              selectedImageName:@"tab1"];
    homeNavigaionController.tabBarItem = tabBarItem;
    [viewControllers addObject:homeNavigaionController];
  }

  {
   // Now
    NowViewController* viewController = [[NowViewController alloc] init];
    viewController.isTabBarChildController = YES;
    viewController.edgesForExtendedLayout = UIRectEdgeTop | UIRectEdgeLeft | UIRectEdgeRight;
    nowNavigaionController = [CCNavigationController
        transparentNavigationControllerWithRootViewController:viewController];
    UITabBarItem* tabBarItem = [self tabBarItem:@"此刻"
                                      imageName:@"tab2_"
                              selectedImageName:@"tab2"];
    nowNavigaionController.tabBarItem = tabBarItem;
    [viewControllers addObject:nowNavigaionController];
  }

  {
    // Profile
    ProfileViewController* viewController = [[ProfileViewController alloc] init];
    viewController.isTabBarChildController = YES;
    viewController.edgesForExtendedLayout = UIRectEdgeTop | UIRectEdgeLeft | UIRectEdgeRight;
    profileNavigaionController = [CCNavigationController
        transparentNavigationControllerWithRootViewController:viewController];
    UITabBarItem* tabBarItem = [self tabBarItem:@"我的"
                                      imageName:@"tab3_"
                              selectedImageName:@"tab3"];
    profileNavigaionController.tabBarItem = tabBarItem;
    [viewControllers addObject:profileNavigaionController];
  }
  MainViewController* mainController = [[MainViewController alloc] init];
  _mainController = mainController;
  mainController.delegate = self;
  mainController.viewControllers = viewControllers;
  mainController.selectedIndex = MainTabIndexHome;
  mainController.tabBar.unselectedItemTintColor = LC_COLOR_GRAY;
  CCNavigationController* rootNavigationController = [CCNavigationController
      transparentNavigationControllerWithRootViewController:_mainController];
  rootNavigationController.navigationBarHidden = YES;
  return rootNavigationController;
}

- (void)doLogoutAction {
}

- (void)loginedDoShowWindow {
  UIViewController* viewController = [self generateMainViewController];
  [UIView transitionFromView:self.mainWindow.rootViewController.view
                      toView:viewController.view
                    duration:0.5f
                     options:UIViewAnimationOptionCurveLinear
                  completion:^(BOOL finished) {
                    self.mainWindow.rootViewController = viewController;
                  }];
}

- (MainTabIndex)selectedIndex {
  return self.mainController.selectedIndex;
}

- (void)selectedIndex:(MainTabIndex)tabIndex {
  self.mainController.selectedIndex = tabIndex;
}

- (void)switchToTabIndex:(MainTabIndex)tabIndex {
  [self.mainController switchToTabIndex:tabIndex pushViewController:nil];
}

- (void)switchToTabIndex:(MainTabIndex)tabIndex
      pushViewController:(UIViewController*)toViewController {
  [self.mainController switchToTabIndex:tabIndex pushViewController:toViewController animated:YES];
}

- (void)switchToTabIndex:(MainTabIndex)tabIndex
      pushViewController:(UIViewController*)toViewController
                animated:(BOOL)animated {
  [self.mainController switchToTabIndex:tabIndex
                     pushViewController:toViewController
                               animated:animated];
}

- (void)switchToTabIndex:(MainTabIndex)tabIndex
     pushViewControllers:(NSArray*)viewControllers
                animated:(BOOL)animated {
  [self.mainController switchToTabIndex:tabIndex
                    pushViewControllers:viewControllers
                               animated:animated];
}

- (void)currentTabPushViewController:(UIViewController*)toViewController {
  [self switchToTabIndex:self.selectedIndex pushViewController:toViewController];
}

- (void)removeViewControllers:(IsControllerNeedRemoveHandler)handler {
  if (handler == nil) {
    return;
  }
  UINavigationController* selectedNavigationController =
      [self.mainController selectedViewController];
  if (![selectedNavigationController isKindOfClass:UINavigationController.class]) {
    selectedNavigationController = selectedNavigationController.navigationController;
  }
  if (selectedNavigationController != nil) {
    NSMutableArray* viewControllers =
        [NSMutableArray arrayWithArray:selectedNavigationController.viewControllers];
    for (NSInteger i = viewControllers.count - 1; i >= 0; i--) {
      UIViewController* viewController = viewControllers[i];
      if (handler(viewController)) {
        [viewControllers removeObjectAtIndex:i];
      }
    }
    if (viewControllers.count != selectedNavigationController.viewControllersCount) {
      [selectedNavigationController setViewControllers:viewControllers animated:YES];
    }
  }
}

- (void)tabBarController:(UITabBarController*)tabBarController
    didSelectViewController:(UIViewController*)viewController {
}

#pragma mark - util method

- (UITabBarItem*)tabBarItem:(NSString*)title
                  imageName:imageName
          selectedImageName:selectedImageName {
  UIImage* image =
      [[UIImage imageNamed:imageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
  UIImage* selectedImage = [[UIImage imageNamed:selectedImageName]
      imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
  UITabBarItem* barItem =
      [[UITabBarItem alloc] initWithTitle:title image:image selectedImage:selectedImage];
  LcFont* barFont = [LcFont rr11];
  barItem.titlePositionAdjustment = UIOffsetMake(0, 0);
  [barItem setTitleTextAttributes:@{
    NSForegroundColorAttributeName : LC_COLOR_BACKGROUND_BLUE_GRAY_1,
    NSFontAttributeName : barFont.font
  }
                         forState:UIControlStateNormal];
  [barItem setTitleTextAttributes:@{
    NSForegroundColorAttributeName : LC_COLOR_BACKGROUND_BLUE_GRAY_3,
    NSFontAttributeName : barFont.font
  }
                         forState:UIControlStateSelected];
  return barItem;
}

- (void)notificationReceived:(NSString*)notification auxiliary:(id)auxiliary {
}

@end

